<?php

namespace app\job;

//支付通执行回调操作
use app\service\curlService;
use app\service\RedisQueueService;
use app\service\serviceService;
use think\facade\Db;
use think\queue\Job;

class ZftNotifyUrl
{
    public $jobData;

    public function __construct($jobData)
    {
        $this->jobData = $jobData;
    }

    /**
     * fire方法是消息队列默认调用的方法
     * @param Job            $job      当前的任务对象
     * @param array|mixed    $data     发布任务时自定义的数据
     */
    public function fire(Job $job,$data){
        // 如有必要,可以根据业务需求和数据库中的最新数据,判断该任务是否仍有必要执行.
        //执行收单交易结算

        $isJobDone = $this->doJob($this->jobData);
        print("<info>ZftNotifyUrlOrder Job Started. job Data is: ".var_export($isJobDone,true)."</info> \n");

        if ($isJobDone) {

            $job->delete();
        }else{
            print("<info>ZftNotifyUrlOrderattempts: ".$job->attempts()."</info> \n");
            if($job->attempts()==1){
                $job->release(300);
            }else if($job->attempts()==2){
                $job->release(3600);
            }else if($job->attempts()==3){
                $job->release(3600*24);
            }else if ($job->attempts() > 3) {
                //记录开票推送失败的记录
                $job->delete();
                //$job->release(300);
                (new RedisQueueService())->push("ZftNotifyUrlOrderIdFailed",$data["id"]);

            }else{
                $job->release(10);
            }
        }
    }

    public function doJob($data){
        $url=(new serviceService())->getNotifyUrlByComId($data["community_id"]);
        $res=(new curlService())->curl_post($url,$data["alipay_notify_con"],[]);
        print("<info>ZftNotifyUrl Job Started. job Data is: ".var_export($res,true)."</info> \n");
        if($res=="success"){
            return true;
        }else{
            return false;
        }

    }

}